<?php
/**
 * User: Allen
 * Date: 15-07-10
 * Time: 14:21
 * 师傅
 */

class Master extends PC_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->load->model('master_model');
		$this->load->model('appoint_model');
		$this->load->model('area_model');
		$this->lang->load('appoint');
	}
	
	//师傅列表
	public function master_list() {
		//获取参数
		$where['check_flag'] = $this->input->get('check_flag') ? $this->input->get('check_flag') : '1';
		//获取总数
		$this->data['count'] = $this->master_model->getCount($where);
		if($this->data['count']) {
			$this->data['data'] = $this->master_model->getMasterlist($where);
			foreach($this->data['data'] as $key => $value) {
				$classes = unserialize($value['classes']);
				$areas = unserialize($value['areas']);
				$areas = $areas['detail'];
				if(is_array($classes)) {
					$tmp_classes = '';
					foreach($classes as $k => $v) {
						$tmp_classes .= $v." ";
					}
					$classes = $tmp_classes;
				}										
				$this->data['data'][$key]['classes'] = $classes;
				$this->data['data'][$key]['areas'] = $areas;
				$this->data['data'][$key]['create_time'] = date("Y-m-d",$value['update_time']);
				$this->data['data'][$key]['check_url'] ='													
													<a href="edit_page/'.$value['id'].'"><span class="btn btn-sm btn-pink">编辑</span></a>
													<a href="del/'.$value['id'].'"><span class="btn btn-sm btn-warning cancel">删除</span></a>
													<a href="view_order?id='.$value['id'].'"><span class="btn btn-sm btn-primary">查看订单</span></a>';				
				
			}
		}else {
			$this->data['data'] = array();
		}
		$this->data['top_title'] = '师傅列表 Master List';
		$this->data['time_title'] = '加入时间';
		$this->load->view('/www/master_list.php',$this->data);
	}
	
	//申请师傅列表
	public function apply_list() {
		//获取参数
		$where['check_flag'] = $this->input->get('check_flag') ? $this->input->get('check_flag') : '2';
		//获取总数
		$this->data['count'] = $this->master_model->getCount($where);
		if($this->data['count']) {
			$this->data['data'] = $this->master_model->getMasterlist($where);
			foreach($this->data['data'] as $key => $value) {
				$classes = unserialize($value['classes']);
				$areas = unserialize($value['areas']);
				$areas = $areas['detail'];
				if(is_array($classes)) {
					$tmp_classes = '';
					foreach($classes as $k => $v) {
						$tmp_classes .= $v." ";
					}
					$classes = $tmp_classes;
				}
				$this->data['data'][$key]['classes'] = $classes;
				$this->data['data'][$key]['areas'] = $areas;
				$this->data['data'][$key]['create_time'] = date("Y-m-d",$value['create_time']);
				$this->data['data'][$key]['check_url'] = '<a href="check?id='.$value['id'].'&status=1"><span class="btn btn-sm btn-success audit">审核通过</span></a>
														  <a href="check?id='.$value['id'].'&status=3"><span class="btn btn-sm btn-warning pass">审核不通过</span></a>';				
			}
		}else {
			$this->data['data'] = array();
		}
		$this->data['top_title'] = '申请列表 Apply List';
		$this->data['time_title'] = '申请时间';
		$this->load->view('/www/master_list.php',$this->data);
	}
	
	//读取信息进入编辑页面
	public function edit_page() {
		$id = $this->uri->segment(4);
		$where['id'] = $id;
		$master_info = $this->master_model->checkMaster($where);
		$areas = unserialize($master_info['areas']); 
		$area_array = explode(',', $areas['area_no']);
		$province_area = $area_array[0];
		$city_area = $area_array[1];
		$area_where['parentno'] = 0;
		$area_list = $this->area_model->getArealist($area_where);
		$city_list = $this->area_model->getArealist(array('parentno'=>$province_area,'arealevel'=>2));
		$county_list = $this->area_model->getArealist(array('parentno'=>$city_area,'arealevel'=>3));
		$this->data['data'] = $master_info;
		$this->data['area_list'] = $area_list;
		$this->data['city_list'] = $city_list;	
		$this->data['county_list'] = $county_list;
		$this->data['area_array'] = $area_array;
		$this->load->view('/www/master_edit',$this->data);
	}
	
	//获取城市
	public function city() {
		$id = $this->uri->segment(4);
		$city_list = $this->area_model->getArealist(array('parentno'=>$id,'arealevel'=>2));
		$county_list = $this->area_model->getArealist(array('parentno'=>$city_list[0]['areano'],'arealevel'=>3));
		$city ='';
		$county = '';
		foreach($city_list as $value) {
			$city .= ' <option value="'.$value['areano'].','.$value['areaname'].'">'.$value['areaname'].'</option>';
		}
		foreach($county_list as $value) {
			$county .= ' <option value="'.$value['areano'].','.$value['areaname'].'">'.$value['areaname'].'</option>';
		}
		$data['city'] = $city;
		$data['county'] = $county;
		echo json_encode($data);
	}
	
	//获取区域
	public function county() {
		$id = $this->uri->segment(4);
		$county_list = $this->area_model->getArealist(array('parentno'=>$id,'arealevel'=>3));
		$county = '';
		foreach($county_list as $value) {
			$county .= ' <option value="'.$value['areano'].','.$value['areaname'].'">'.$value['areaname'].'</option>';
		}
		$data['county'] = $county;
		echo json_encode($data);
	}
	
	//删除操作
	public function del() {
		$id = $this->uri->segment(4);
		$where['id'] = $id;	
		$del_result = $this->master_model->deleteMaster($where);
		if($del_result) {
			echo "<script>alert('删除成功');window.location.replace('/www/master/master_list');</script>";
		}else {
			echo "<script>alert('删除失败');window.location.replace('/www/master/master_list');</script>";
		}
	}
	
	//审核操作
	public function check() {
		$data = $_GET;
		$where['id'] = $data['id'];
		$master['check_flag'] = $data['status'];
		$check_result = $this->master_model->editMaster($where,$master);
		if($data['status'] == 1) {
			$master_info = $this->master_model->checkMaster($where);
			$user_data['username'] = $master_info['name'];
			$user_data['phone'] = $master_info['phone'];
			$user_where['id'] = $master_info['user_id'];
			$this->load->model('user_model');
			$edit_result = $this->user_model->editUser($user_where,$user_data);
		}
		if($check_result) {
			echo "<script>alert('审核成功');window.location.replace('/www/master/apply_list');</script>";
		}else {
			echo "<script>alert('审核失败');window.location.replace('/www/master/apply_list');</script>";
			
		}
	}
	
	//读取师傅服务项目
	public function master_project() {
		$id = $this->uri->segment(4);
		$where['id'] = $id;
		$master_info = $this->master_model->checkMaster($where);
		$datainfo = '';
		$classes = unserialize($master_info['classes']);
		if(is_array($classes)) {
			foreach($classes as $key => $value) {
				$datainfo .= '<li>
								<div class="tag-editor-spacer">&nbsp;,</div>
								<div class="tag-editor-tag">'.$value.'</div>
								<div class="tag-editor-delete"><i></i></div>
							 </li>';
			}
		}else {
			$datainfo = '<li>
							<div class="tag-editor-spacer">&nbsp;,</div>
							<div class="tag-editor-tag">'.$classes.'</div>
							<div class="tag-editor-delete"><i></i></div>
						</li>';
		}
		echo json_encode($datainfo);
	}
	
	//编辑操作
	public function edit() {
		$data = $_POST;
		$province = $data['province'];
		$city = $data['city'];
		$county = $data['county'];
		$province = explode(",", $province);
		$city = explode(",", $city);
		$county = explode(",", $county);
		$areas['area_no']= $province[0].','.$city[0].','.$county[0];
		$areas['detail']= $province[1].$city[1].$county[1];
		$data['areas'] = serialize($areas);
		unset($data['province']);unset($data['city']);unset($data['county']);
		if(!empty($data['pname'])) {
			$pname = explode(',',$data['pname']);
			$data['classes'] = serialize($pname);
		}else {
			unset($data['pname']);
		}
		$data['update_time'] = time();
		unset($data['old_areas']);
		$where['id'] = $data['id'];
		$edit_result = $this->master_model->editMaster($where,$data);
		if($edit_result) {
			echo "<script>alert('编辑成功');window.location.replace('/www/master/master_list');</script>";
		}else {
			echo "<script>alert('编辑失败');window.location.replace('/www/master/master_list');</script>";
		}
	}
	
	//查看个人订单列表
	public function view_order() {
		//获取参数
		$data = $_GET;	
		if(isset($data['status']) && $data['status'] > 0) {
			$where['appoint_status'] = $data['status'];
		}else {
			$data['status'] = 0;
		}
		$where['master_id'] = $data['id'];
		//获取师傅名
		$master_where['id'] = $data['id'];
		$master_info = $this->master_model->checkMaster($master_where);
		$this->data['master_name'] = $master_info['name'];
		//获取订单列表		
		$this->data['data'] = $this->appoint_model->masterAppoint($where);
		foreach($this->data['data'] as $key => $value) {
			$status_id = $value['appoint_status'];
			$this->data['data'][$key]['status'] = $this->lang->line($status_id+5);
			if($value['pay_type'] == 1) {
				$this->data['data'][$key]['pay_type'] = '线下支付';
			}					
		}
		$this->data['master_id'] = $data['id'];
		$this->data['status'] = $data['status'];
		$this->load->view('/www/master_appoint.php',$this->data);
	}	
	
	//进入添加页面
	public function add_page() {
		$this->load->view('/www/master_add',$this->data);
	}
	
	//添加假人操作
	public function add() {
		$data = $_POST;	
		$data['check_flag'] = 1;
		$data['person_flag'] = 2;
		$data['create_time'] = $data['update_time'] = time();
		$add_result = $this->master_model->addMaster($data);
		if($add_result) {
			echo "<script>alert('编辑成功');window.location.replace('/www/master/master_list');</script>";
		}else {
			echo "<script>alert('编辑失败');window.location.replace('/www/master/master_list');</script>";
		}
	}
}